import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {UtilityService} from '../../../../service/utils.service';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {ActivatedRoute, Router} from '@angular/router';
import {appConfig} from '../../../../service/common';

@Component({
  selector: 'app-data-range',
  templateUrl: './data-range.component.html',
})
export class DataRangeComponent implements OnInit {
    roleGuid: string;
    constructor(
        private http: _HttpClient,
        private router: Router,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
        private nznot: NzNotificationService,
        private utilityService: UtilityService,
    ) { }
    roleName: string;
    roleCode: string
    ngOnInit() {
        this.roleGuid = this.activatedRoute.snapshot.params.id; // 拿到父组件传过来的组织机构的guid来进行操作
        console.log();
        this.queryRoleData(this.roleGuid)
        this.getData();
    }
     headerData = [  // 配置表头内容
        {value: '数据实体', key: 'dataEntity',  isclick: false},
        {value: '应用名称' , key: 'applyName', isclick: true},
        {value: '可查看数据范围' , key: 'viewRange', isclick: true},
        {value: '可管理数据范围', key: 'manageableRange',  isclick:  false},
        {value: '自定义数据范围' , key: 'customRange', isclick: false},
    ];

    headerDataAdd = [
        {value: '实体名称', key: 'dataEntity',  isclick: false},
        {value: '对应表名称' , key: 'tableName', isclick: true},
        {value: '应用名称' , key: 'applyName', isclick: true},
        {value: '实体描述信息', key: 'entityDescription',  isclick:  false},

    ]
    data=[];
    dataAdd=[];
    showAdd = false;
    showAddAdd = true;
    searchValue = '';
    dataAddModal = false; //新增模态框
    // loading = true;
    //搜索
    isNull = true;// 是否有数据
    total:number;
    totalAdd:number;
    pageindex:number;
    pageindexAdd:number;
    operate = false;//操作列
    buttonsRange=[
           {key: 'custom', value: '自定义数据范围', if: false},
           {key: 'dels', value: '删除', if: false},
        ]
        moreData = {
            buttons:'删除'
        }


    // 根据guid查询角色消息
    queryRoleData(id) {
        this.utilityService.getData(appConfig.serverUrl +  appConfig.API.queryRoleById + '/' + id)
            .subscribe(
                (val) => {
                    console.log(val)
                    this.roleName = val.result.roleName;
                    this.roleCode = val.result.roleCode;
                });
    }


    getData(){
        this.pageindex = 1;
        this.utilityService.getData(appConfig.serverUrl + '/' + appConfig.API.dataRangeList)
            .subscribe(
                (val) => {
                    console.log(val)
                    this.data = val
                    this.total = val.length;
                    for(let i = 0; i < this.data.length ; i++){
                        this.data[i]['buttonData'] = this.buttonsRange;
                    }

                    console.log(this.data)
                    // this.loading = false;

                });
    }
    // 翻页
    monitorHandler(event){
         console.log(event);
         this.pageindex = event;
        }
     onSearch(event: string): void {
            console.log(event);
        }
        // 新增数据范围
        addRange(){
          this.dataAddModal = true;
          this.getDataAdd();

        }
        getDataAdd(){
              this.utilityService.getData(appConfig.serverUrl + '/' + appConfig.API.dataRangeListAdd)
            .subscribe(
                (val) => {
                    console.log(val)
                    this.dataAdd = val
                    this.totalAdd = val.length;
                    this.pageindexAdd = 1;
                    // this.loading = false;

                });
        }
        monitorHandlerAdd(event){
            console.log(event)
            this.pageindexAdd = event;

         this.getDataAdd();
        }

        // list列表按钮
        buttonEvent(event){
            switch(event.names.key){
                case 'dels':
                console.log('这是删除方法')
                break;
                case 'custom':
                console.log('这是自定义数据范围')
                break;
            }

        }
        // 新增数据提交
        subAdddata(){

        }
        checkeds:any[];
        selectedRowAdd(event){
           this.checkeds = event.selectedRows //选中的数组
            //   this.utilityService.postData(appConfig.serverUrl + '/' + appConfig.API.dataRangeListAdd,{})
            //      .subscribe(
            //     (val) => {
            //         console.log(val)
            //     });
            console.log(event)
        }

}
